package com.zhuangjie.shop.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhaungjie.base.conduit.R;
import com.zhuangjie.base.constant.CommonSeparatorConstants;
import com.zhuangjie.security.annotations.NoAuthentication;
import com.zhuangjie.security.intercepor.UserRequestInterceptor;
import com.zhuangjie.shop.dto.DishCategorySaveDTO;
import com.zhuangjie.shop.dto.ShopCategoryPageDTO;
import com.zhuangjie.shop.entity.DishCategory;
import com.zhuangjie.shop.helper.ShopModuleTokenDataProvider;
import com.zhuangjie.shop.service.DishCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author zhuangjie
 * @since 2023-09-24
 */
@RestController
@RequestMapping("/dish-category")
@Api(tags = "菜品分类的相关API")
public class DishCategoryController{

    @Autowired
    private DishCategoryService dishCategoryService;

    @Autowired
    private UserRequestInterceptor userRequestInterceptor;

    /**
     * 菜品分页查询接口
     * @param pageInput 会将get请求的属性映射到该类的属性上
     * @return
     * @throws Exception
     */
    @GetMapping("/queryShopCategoryListPage")
    @NoAuthentication
    public R queryShopCategoryListPage(@Valid ShopCategoryPageDTO pageInput, @RequestParam(required = false) Long shopId) throws Exception {
        Page<DishCategory> page = dishCategoryService.queryShopCategoryListPage(pageInput,shopId);
        // 获取分页查询的数据
        List<DishCategory> dishCategoryList = page.getRecords();
        // 前端要完成分页，需要返回下面两个数据
        return R.ok().dataAsMap(m->{
            m.put("listData",dishCategoryList);
            m.put("total",page.getTotal());
        });
    }

    /**
     * 列出商店菜品类别
     *
     * @param shopId 店铺id
     * @return {@link R}
     * @throws Exception 例外
     */
    @GetMapping("/listShopDishCategory")
    @NoAuthentication
    @ApiOperation("获取店铺的菜品分类列表")
    public R listShopDishCategory(@RequestParam(required = false) Long shopId) throws Exception {
        List<DishCategory> shopDishCategoryList = dishCategoryService.listShopDishCategory(shopId);
        return R.ok().data(shopDishCategoryList);
    }

    @GetMapping("/dish-category-tree")
    @ApiOperation("获取店铺的菜品分类树")
    @NoAuthentication
    public R dishCategoryTree(@RequestParam(required = false) Long shopId) throws Exception {
        if (shopId == null && (userRequestInterceptor.tryMountCurrentMountLoginState() == null || (shopId = ShopModuleTokenDataProvider.getShopId()) == null)) throw new RuntimeException("获取类目-商品分类失败！，因为无法获取店铺id！");
        return R.require(dishCategoryService.getShopDishCategory(shopId,false));
    }



    /**
     * 按id删除商店菜品分类
     *
     * @param dishCategoryId 菜类id
     * @return {@link R}
     */
    @PostMapping("/removeShopDishCategory/{dishCategoryId}")
    @ApiOperation("按id删除商店菜品分类")
    public R removeShopDishCategoryById(@NotNull(message = "菜品分类id不能为空！") @PathVariable("dishCategoryId") Long dishCategoryId) {
        return R.auto(dishCategoryService.removeShopDishCategoryById(dishCategoryId));
    }

    /**
     * 按id获取商店菜品类别
     *
     * @param dishCategoryId 菜类id
     * @return {@link R}
     */
    @GetMapping("/getShopDishCategory/{dishCategoryId}")
    @ApiOperation("按id获取商店菜品类别")
    public R getShopDishCategoryById(@NotNull(message = "菜品分类id不能为空！") @PathVariable("dishCategoryId") Long dishCategoryId) {
        DishCategory dishCategory =  dishCategoryService.getShopDishCategoryById(dishCategoryId);
        return R.ok().data(dishCategory);
    }

    @PostMapping("/saveDishCategory")
    @ApiOperation("保存菜品分类")
    public R saveDishCategory(@Valid @RequestBody DishCategorySaveDTO dishCategorySaveDTO) {
        return R.auto(dishCategoryService.saveDishCategory(dishCategorySaveDTO));
    }

    @GetMapping("/category-name")
    @ApiOperation("根据分类id查询分类名称")
    public R queryCategoryNames(@RequestParam("dishCategoryIds") @NotNull(message = "菜品分类id不能为空！") String dishCategoryIds) {
        List<Long> dishCategoryIdList = Arrays.stream(dishCategoryIds.split(CommonSeparatorConstants.MULTIPLE_SEPARATOR)).map(Long::valueOf).collect(Collectors.toList());
        return R.ok().data(dishCategoryService.queryDishCategoryNameByIds(dishCategoryIdList));
    }

}

